package com.sinitek.common.support;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * Created by IntelliJ IDEA.
 * User: 王志华
 * Date: 2018/2/18
 * Time: 下午11:57
 * To change this template use File | Settings | File Templates.
 */
@Data
@ApiModel(description = "请求结果")
public class RequestResult<T> {

    @ApiModelProperty(value = "错误消息")
    private String message = null;

    @ApiModelProperty(value = "消息代码")
    private String resultcode = null;

    @ApiModelProperty(value = "数据")
    private T data = null;

    /**
     * 默认失败代码
     */
    public static final String FAIL_CODE = "-1";

    /**
     * 成功代码
     */
    public static final String SUCCESS_CODE = "0";

    private RequestResult(String resultCode, String message, T data) {
        this.message = message;
        this.resultcode = resultCode;
        this.data = data;
    }

    public static<T> RequestResult<T> success() {
        return new RequestResult<>(SUCCESS_CODE, null, null);
    }

    public static<T> RequestResult<T> success(T data) {
        return new RequestResult<>(SUCCESS_CODE, null, data);
    }

    public static<T> RequestResult<T> fail() {
        return new RequestResult<>(FAIL_CODE, null, null);
    }

    public static<T> RequestResult<T> fail(String resultCode) {
        return new RequestResult<>(resultCode, null, null);
    }

    public static<T> RequestResult<T> fail(String resultCode, T data) {
        return new RequestResult<>(resultCode, null, data);
    }

    public static<T> RequestResult<T> fail(String resultCode, String message, T data) {
        return new RequestResult<>(resultCode, message, data);
    }

    /**
     * 判断当前RequestResult是否成功
     * @return
     */
    public boolean isSuccess() {
        return SUCCESS_CODE.equals(resultcode);
    }
}

